<?php

namespace App\Admin\Controllers;

use App\Models\Device;
use App\Models\SmsLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SmsLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '短信';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SmsLog());

        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('Id'));
        $grid->column('device_id', __('Device id'));
//        $grid->column('device_code', __('Device code'));
        $grid->column('got_at', __('Got at'));
//        $grid->column('updated_at', __('Updated at'));
        $grid->column('phone_number', __('Phone number'));
        $grid->column('message', __('Message'));
        $grid->column('from_number', __('From number'));
        $grid->column('sim', __('Sim'));
        $grid->column('created_at', __('Created at'))->sortable();;
//        $grid->column('sms_center', __('Sms center'));
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->equal('device_id', __('Device id'))->select(Device::all()->pluck('full_tag','device_id'));
            $filter->equal('phone_number', __("Phone number"))->mobile();
            $filter->like('message', __("Message"));
            $filter->between('got_at', __('Got at'))->datetime();;
            $filter->between('created_at', __('Created at'))->datetime();;

        });
        $grid->disableCreateButton();

        $grid->actions(function ($actions) {

            // 去掉删除
//            $actions->disableDelete();
            // 没有`delete-image`权限的角色不显示删除按钮
            if (!Admin::user()->can('delete-image')) {
                $actions->disableDelete();
            }
            // 去掉编辑
            $actions->disableDelete();
            $actions->disableEdit();
            // 去掉查看
//            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SmsLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('device_id', __('Device id'));
        $show->field('device_code', __('Device code'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('phone_number', __('Phone number'));
        $show->field('message', __('Message'));
        $show->field('from_number', __('From number'));
        $show->field('sim', __('Sim'));
        $show->field('sms_center', __('Sms center'));
        $show->field('got_at', __('Got at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SmsLog());

        $models = Device::all();
        $list = [];
        foreach ($models as $model) {
            $list[$model->id] = "$model->name $model->code";

        }
        $form->select('device_id', __('Device id'))->options($list)->required();
        $form->mobile('phone_number', __('Phone number'))->required();
        $form->text('message', __('Message'))->required();
        $form->number('from_number', __('From number'))->required();
        $form->text('sim', __('Sim'))->required();
        $form->text('sms_center', __('Sms center'))->required();
        $form->datetime('got_at', __('Got at'))->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
